let express = require('express')
let qs = require('querystring')
let movie = express.Router();
let {pool} = require('../db/pool')
// var session = require('express-session');
const jwt = require('jsonwebtoken');
const { connect } = require('http2');
movie.post('/movie/info', (req, res) => {
    let movie_id = req.body.movie_id;
    let keyword = req.body.keyword;
    // console.log(req.query)
    console.log(req.body)
    // console.log(keyword)
    pool.getConnection((err, connection) => {
        if (err) throw err;
     if (movie_id === '') {
         console.log(keyword)
            connection.query(`select * from movie_message where movie_name='${keyword}'`, (err, result) => {
                if(err) throw err;
                else{
                    if (result.length) {
                    res.send(result)
                } else {
                    res.send({
                        status: 300,
                        message: '未找到匹配的内容name'
                    })
                }
                }
                connection.release() 
                // console.log(result)
            })

        }else if(keyword === ''){
            connection.query(`select * from movie_message where movie_id='${movie_id}'`, (err, result) => {
                if (err) throw err;
                else{
                    if (result.length) {
                    res.send(result)
                    // console.log(res)
                } else {
                    res.send({
                        status: 300,
                        message: '未找到匹配的内容id'
                    })
                }
                connection.release() 
                }               
            })        
        }        
    })
})
movie.get('/movie/showing', (req, res) => {
    // console.log(req)
    pool.getConnection((err, connection) => {
        if (err) throw err;
        connection.query('select * from movie_message', (err, result) => {
            if (err) throw err;
            res.send(result)
            // console.log(result)
            connection.release() 
        })
    })
})
movie.get('/movie/showingGood', (req, res) => {
    // console.log(req)
    pool.getConnection((err, connection) => {
        if (err) throw err;
        connection.query(`select * from movie_message where douban_grade>=8.0`, (err, result) => {
            if (err) throw err;
            res.send(result)
            // console.log(result)
            connection.release() 
        })
    })
})
movie.get('/movie/showingChina', (req, res) => {
    // console.log(req)
    pool.getConnection((err, connection) => {
        if (err) throw err;
        connection.query(`select * from movie_message where country='中国'`, (err, result) => {
            if (err) throw err;
            res.send(result)
            // console.log(result)
            connection.release() 
        })
    })
})
movie.get('/movie/showingOuMei', (req, res) => {
    // console.log(req)
    pool.getConnection((err, connection) => {
        if (err) throw err;
        connection.query(`select * from movie_message where country='美国'`, (err, result) => {
            if (err) throw err;
            res.send(result)
            // console.log(result)
            connection.release() 
        })
    })
})
movie.get('/movie/showingKorea', (req, res) => {
    // console.log(req)
    pool.getConnection((err, connection) => {
        if (err) throw err;
        connection.query(`select * from movie_message where country='韩国'`, (err, result) => {
            if (err) throw err;
            res.send(result)
            // console.log(result)
            connection.release() 
        })
    })
})
movie.get('/movie/showingJapan', (req, res) => {
    // console.log(req)
    pool.getConnection((err, connection) => {
        if (err) throw err;
        connection.query(`select * from movie_message where country='日本'`, (err, result) => {
            if (err) throw err;
            res.send(result)
            // console.log(result)
            connection.release() 
        })
    })
})
movie.post('/movie/writeComment',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        // console.log(req.query.username)
        console.log(req.body)
        connection.query(`select user_id from base_user where user_name='${req.body.username}'`,(err,result)=>{
            if(err) throw err;
            let user_id = result[0].user_id;
            let comment_id = Math.floor(Math.random()*100000000);
            connection.query(`insert into movie_comment (id,content,create_time,user_id,movie_id) 
            values ('${comment_id}','${req.body.content}','${Date.now()}','${user_id}','${req.body.movie_id}')`,
            (err,result)=>{
                if(err) throw err;
                console.log(result)
                res.send(result)
            }) 
            connection.release();
        })

    })
})
movie.get('/movie/comment/findAll',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        connection.query(`select * from movie_comment where movie_id='${req.query.movie_id}' order by create_time desc`,(err,result)=>{
            if(err) throw err;
            console.log(req.query)
            res.send(result)
        })
        connection.release();
    })
})
movie.get('/movie/findById',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        let sql = `select movie_name from movie_message where movie_id=${req.query.movie_id}`;
        connection.query(sql,(err,result)=>{
            if(err) throw err;
            res.send(result)
        })
        connection.release()
    })
})
movie.get('/movie/myComment',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        let sql = `select user_id from base_user where user_name='${req.query.username}'`;
        connection.query(sql,(err,result)=>{
            let user_id = result[0].user_id;
            let sql = `select * from movie_comment where user_id='${user_id}'`
            connection.query(sql,(err,result)=>{
                res.send(result)
            })
        })
        connection.release();
    })
})
movie.get('/movie/comment/findById',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        let sql = `select * from movie_comment where id='${req.query.id}'`
        connection.query(sql,(err,result)=>{
            res.send(result)
        })
        connection.release();
    })
})
movie.get('/movie/yuxianSearch',(req,res)=>{
    pool.getConnection((err,connection)=>{
        if(err) throw err;
        let sql = `select movie_name from movie_message where movie_name like '%${req.query.word}%'`
        connection.query(sql,(err,result)=>{
            res.send(result)
        })
        connection.release()
    })
})
module.exports = {
    movie
}